// 游学
layui.use(['jquery', 'element', 'table', 'layer', 'form', 'upload'], function(){
    var $ = layui.$;
    var customerTable = layui.table;
    var ctxPath = "/education";

    customerTable.render({
        id: 'customerTableId',
        elem: '#customerTableId',
        url: ctxPath + '/api/customer/list',
        page: true,
        method: 'post',
        cols:[[
            {field:'id', title:'ID', width: 50},
            {field:'name', title:'名称', width: 200},
            {field:'code', title:'编号', width: 100 },
            {field:'phone', title:'手机号', width: 100 },
            {field:'point', title:'积分', width: 150 },
            {field:'schoolGradeClassName', title:'学校年级班级', width: 250 },
            {field:'type', title:'类型', width: 150 },
            {field:'enabled', title:'是否启用', width: 100 },
            {fixed:'right', title:'操作', width: 200, align:'center', toolbar: '#customerBar'}
        ]],
        response: {
            statusName: 'returnCode' //数据状态的字段名称，默认：code
            ,statusCode: 0 //成功的状态码，默认：0
            ,msgName: 'returnMsg' //状态信息的字段名称，默认：msg
            ,countName: 'count' //数据总数的字段名称，默认：count
            ,dataName: 'data' //数据列表的字段名称，默认：data
        },
        done: function(res, curr, count){
            //如果是异步请求数据方式，res即为你接口返回的信息。
            //如果是直接赋值的方式，res即为：{data: [], count: 99} data为当前页数据、count为数据总长度
            console.log(res);

            //得到当前页码
            console.log(curr);

            //得到数据总量
            console.log(count);
        }
    });

    var active = {
        searchCustomer: function(){
            var code = $('#code').val();
            var name = $('#name').val();
            var phone = $('#phone').val();
            var enabled = $('#enabled').val();
            customerTable.reload('customerTableId', {
                where: {
                    code: code,
                    name: name,
                    phone: phone,
                    enabled: enabled
                }
            });
        },
        cancelBtn: function(e) {
            window.location.href = ctxPath + '/html/customer/customerList.html';
        }
    };

    //按钮事件
    $('.layui-btn').on('click', function(e){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    //监听工具条
    customerTable.on('tool(customer)', function(obj){
        var data = obj.data ,layEvent = obj.event;
        if(layEvent === 'detail'){
            location.href = ctxPath + "/html/customer/viewCustomer.html?id=" + data.id;
        } else if(layEvent === 'edit'){
            location.href = ctxPath + "/html/customer/formCustomer.html?id=" + data.id;
        }
        return false;
    });


});
